package de.rosstauscher.jculfw.tools;

import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import de.rosstauscher.jculfw.JCulFW;
import de.rosstauscher.jculfw.fs20.FS20CommandCode;
import de.rosstauscher.jculfw.fs20.FS20MessageProcessor;

/**********************************************************
 * Application to listen for FS20 commands and dump them to the console.
 * Does only support he CUNO for now 
 * 
 * @author Rossi java@rosstauscher.de
 *********************************************************/

public class FS20Dumper {

	/**********************************************************
     * Usage: java FS20 cuno housecode device code command  
     * @param args
     * @throws IOException
     *********************************************************/
	
    public static void main( String[] args ) throws IOException
    {
    	if (args.length != 1) {
    		System.out.println("Usage: FS20Dumper host:port");
    		return;
    	}
    	String[] host = args[0].split(":");
        Socket socket = new Socket(host[0], Integer.parseInt(host[1]));
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        JCulFW cul = new JCulFW(in, out);
        cul.enableSlowRFListening();

        cul.addMessageProcessor(new FS20MessageProcessor() {
			@Override
			protected void processFS20Message(String houseCode,
					String deviceID, FS20CommandCode command) {
				System.out.println("House Code: "+houseCode+" Device: "+deviceID+" Command: "+command);
			}
		});
        System.out.println("Press a ENTER to stop listening!");
        cul.startMessageProcessing();
        
        Console cons = System.console();
        cons.readLine();
        System.out.println("Bye");
        
        cul.stopMessageProcessing();
    }
}
